package com.yyi.springcloud.controller;


import com.github.pagehelper.PageInfo;
import com.yyi.springcloud.entities.*;
import com.yyi.springcloud.service.Local_SearchInfoService;
import com.yyi.springcloud.service.Local_TestProdInfoItemService;
import com.yyi.springcloud.service.Local_TestProdInfoValueService;
import com.yyi.springcloud.service.Local_TestUnitConfigService;
import com.yyi.springcloud.utils.ReflectionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/mysqlapi/TestProdInfo")
public class Mysql_TestProdInfoController {
	@Resource
	private Local_TestProdInfoItemService testProdInfoItemService;
	@Resource
	private Local_TestProdInfoValueService testProdInfoValueService;
	@Resource
	private Local_TestUnitConfigService testUnitConfigService;
	@Autowired
	private Local_SearchInfoService searchInfoService;

	// 1.获取某个台位的测试元数据(铭牌信息)
	// http://localhost:8080/bxxs/api/TestProdInfo/getTestProdInfo?labcode=Haierrefrigerator20151214&testunitno=6
	@ResponseBody
	@RequestMapping(value = "/getTestProdInfo", method = RequestMethod.GET)
	public List<TestProdInfoVo> getTestProdInfo(HttpServletRequest request, HttpServletResponse response) {
		List<TestProdInfoVo> testProdInfoVoList = new ArrayList<TestProdInfoVo>();

		String labcode = request.getParameter("labcode");
		String testunitno = request.getParameter("testunitno");
		int pageNum = Integer.parseInt(request.getParameter("pageNum") == null ? "1" : request.getParameter("pageNum"));
		int pageSize = Integer
				.parseInt(request.getParameter("pageSize") == null ? "10" : request.getParameter("pageSize"));
		TestProdInfoValueModel testProdInfoValueModel = new TestProdInfoValueModel();
		testProdInfoValueModel.setTestnow(1);// 在测
		testProdInfoValueModel.setLabcode(labcode);
		testProdInfoValueModel.setTestunitno(Integer.parseInt(testunitno == null ? "0" : testunitno));
		PageInfo<TestProdInfoValueModel> allTestProdInfoValuePage = testProdInfoValueService
				.getAllTestProdInfoValueByCondition(testProdInfoValueModel, pageNum, pageSize, null);
		if (allTestProdInfoValuePage.getList() == null || allTestProdInfoValuePage.getList().size() < 1) {
			return null;
		}
		// 查询所有的测试元数据(铭牌信息)
		List<TestProdInfoItemModel> testProdInfoItemList = testProdInfoItemService.getTestProdInfoItemByCondition(null);
		for (TestProdInfoItemModel model : testProdInfoItemList) {
			TestProdInfoVo testProdInfoVo = new TestProdInfoVo();
			testProdInfoVo.setItemName(model.getEnglishname());
			testProdInfoVo.setInputMode(model.getInputmode());
			// 根据铭牌信息的id查询铭牌信息的值
			String testProdInfoValue = getTestProdInfoValueByItemId(model.getItemno(),
					allTestProdInfoValuePage.getList().get(0));
			testProdInfoVo.setItemValue(testProdInfoValue);
			testProdInfoVoList.add(testProdInfoVo);
		}
		return testProdInfoVoList;
	}

	// 2.查询所有的铭牌信息条目
	@ResponseBody
	@RequestMapping(value = "/listTestProdInfoItems", method = RequestMethod.GET)
	public List<TestProdInfoItemModel> listTestProdInfoItems() {
		List<TestProdInfoItemModel> listTestProdInfoItems = testProdInfoItemService.listTestProdInfoItems();
		return listTestProdInfoItems;
	}

	// 根据铭牌信息条目id查询铭牌信息的内容
	public String getTestProdInfoValueByItemId(int testProdInfoItemid, TestProdInfoValueModel testProdInfoValueModel) {
		if (testProdInfoValueModel != null) {
			String testProdInfoValue = "";
			String fieldName = "itemcontent" + testProdInfoItemid;
			testProdInfoValue = (String) ReflectionUtil.getResult(fieldName, testProdInfoValueModel);
			return testProdInfoValue;
		} else {
			return "";
		}
	}

	/**
	 * 查询所有的历史数据表格的表头
	 * 
	 * @Description:
	 * @author 高杨
	 * @date 2018年8月9日 下午4:54:57
	 * @return List<DataTablesColumnDto>
	 */
	@ResponseBody
	@RequestMapping(value = "/listHistoryDataHead", method = RequestMethod.GET)
	public List<DataTablesColumnDto> listHistoryDataHead(HttpServletRequest request, String labcode) {
		List<DataTablesColumnDto> columnDtos = new ArrayList<>();
		// 添加上item表中缺少的 台位名称 是否在测 开测时间 停测时间
		// 1.设置台位名称
		columnDtos.add(new DataTablesColumnDto("testUnitName", "台位名称"));
		// 2.设置是否开始测试
		columnDtos.add(new DataTablesColumnDto("testnow", "是否在测"));
		// 3.设置开测时间
		columnDtos.add(new DataTablesColumnDto("begindatetime", "开测时间"));
		// 4.设置停测时间
		columnDtos.add(new DataTablesColumnDto("enddatetime", "停测时间"));
		// 5.查询出所有的表头，转换成datatable-columns可用类型
		List<TestProdInfoItemModel> testProdInfoItemList = testProdInfoItemService.getTestProdInfoItemByCondition(null);
		for (TestProdInfoItemModel model : testProdInfoItemList) {
			String data=model.getEnglishname();
			//去除空格和非法字符
			if(data.indexOf(" ")!=-1){
				data= data.replaceAll(" ","").replaceAll("\\.","");
			}
			DataTablesColumnDto columnDto = new DataTablesColumnDto(data, model.getItemname());
			columnDtos.add(columnDto);
		}
		// 6.返回数据
		return columnDtos;
	}

	/**
	 * 获取历史数据的值
	 * 
	 * @Description:
	 * @author 高杨
	 * @date 2018年8月10日 下午2:11:39
	 * @return DataTablesPage<TestProdInfoValueVo>
	 */
	@ResponseBody
	@RequestMapping(value = "/listHistoryData", method = RequestMethod.GET)
	public DataTablesPage<TestProdInfoValueVo> listHistoryData(int draw, int start, int length, String orderColumn,
			String orderDir, String search) {
		System.err.println("start:" + start + ";length:" + length + ";orderColumn:" + orderColumn + ";orderDir:"
				+ orderDir + ";search:" + search);
		// 对搜索条件进行解析，包含@则进行起止时间的解析
		String startDate = null;
		String endDate = null;

		// 搜索的情况
		if (search != null) {
			if (search.contains("@")) {
				String[] strings = search.split("@");
				search = strings[0];
				startDate = strings[1];
				endDate = strings[2];
			}
			try {
				return searchInfoService.historyDataSearch(draw, search, start, length, startDate, endDate);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		// 1.数据转换，将排序字段转换成对应的DO
		String order = null;
		if (orderColumn != null && orderDir != null) {
			String columName = getColumName(orderColumn);
			order = columName + " " + orderDir;
		}

		// 2.查询出所有的历史数据
		// 计算页码
		int pageNumber = start / length + 1;
		PageInfo<TestProdInfoValueModel> pageInfo = testProdInfoValueService
				.getAllTestProdInfoValueByCondition(new TestProdInfoValueModel(), pageNumber, length, order);

		// 2.遍历数据，替换数据名称
		List<TestProdInfoValueModel> list = pageInfo.getList();
		List<TestProdInfoValueVo> lastTestProdInfoValueVos = new ArrayList<TestProdInfoValueVo>();
		for (TestProdInfoValueModel model : list) {
			TestUnitConfigModel testUnitConfigModel = new TestUnitConfigModel();
			testUnitConfigModel.setTestunitno(model.getTestunitno());
			// 根据编号查出台位名称
			String testUnitName = "";
			List<TestUnitConfigModel> testUnitConfigModels = testUnitConfigService
					.getTestUnitByCondition(testUnitConfigModel);
			if (testUnitConfigModels.size() > 0) {
				testUnitName = testUnitConfigModels.get(0).getTestunitname();
			}

			// 设置是否开测
			String isTest = "否";
			if (model.getTestnow() == 1) {
				isTest = "是";
			}

			lastTestProdInfoValueVos.add(new TestProdInfoValueVo(testUnitName, model.getBegindatetime(),
					model.getEnddatetime(), isTest, model.getItemcontent1(), model.getItemcontent2(),
					model.getItemcontent3(), model.getItemcontent4(), model.getItemcontent5(), model.getItemcontent6(),
					model.getItemcontent7(), model.getItemcontent8(), model.getItemcontent9(), model.getItemcontent10(),
					model.getItemcontent11()));
		}
		DataTablesPage<TestProdInfoValueVo> dataTablesPage = new DataTablesPage<TestProdInfoValueVo>(draw,
				pageInfo.getTotal(), pageInfo.getTotal(), lastTestProdInfoValueVos, length);

		// 3.返回
		return dataTablesPage;
	}

	/**
	 * 实现属性名和表中字段名的转换
	 * 
	 * @Description:
	 * @author 高杨
	 * @date 2018年8月13日 下午4:26:43
	 * @return String
	 */
	private String getColumName(String property) {
		if ("ReportNo".equals(property)) {
			return "ITEMCONTENT_1";
		} else if ("SampleNo".equals(property)) {
			return "ITEMCONTENT_2";
		} else if ("Model".equals(property)) {
			return "ITEMCONTENT_3";
		} else if ("Test".equals(property)) {
			return "ITEMCONTENT_4";
		} else if ("ProductName".equals(property)) {
			return "ITEMCONTENT_5";
		} else if ("ClimateType".equals(property)) {
			return "ITEMCONTENT_6";
		} else if ("AuthorizedUnit".equals(property)) {
			return "ITEMCONTENT_7";
		} else if ("TestPurpose".equals(property)) {
			return "ITEMCONTENT_8";
		} else if ("TestStandard".equals(property)) {
			return "ITEMCONTENT_9";
		} else if ("ProjectDescription".equals(property)) {
			return "ITEMCONTENT_10";
		} else if ("Remark".equals(property)) {
			return "ITEMCONTENT_11";
		} else if ("testUnitName".equals(property)) {
			return "ITEMCONTENT_1";
		} else {
			return property;
		}
	}
}
